//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.gui.iconBar;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.*;
import org.jpgolay.gui.fixed.Messages;

// Referenced classes of package org.jpgolay.gui.iconBar:
//            AnswerListener, AnswerEvent
public class IconBar extends JPanel
        implements Serializable {

    public IconBar() {
        help = true;
        open = true;
        create = true;
        save = true;
        remove = true;
        modify = true;
        print = true;
        initialize();
        listeners = new Vector();
    }

    public IconBar(boolean help, boolean open, boolean create, boolean save, boolean remove, boolean modify, boolean print) {
        this.help = true;
        this.open = true;
        this.create = true;
        this.save = true;
        this.remove = true;
        this.modify = true;
        this.print = true;
        this.help = help;
        this.open = open;
        this.create = create;
        this.save = save;
        this.remove = remove;
        this.modify = modify;
        this.print = print;
        initialize();
        listeners = new Vector();
    }

    private void initialize() {
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setHgap(4);
        flowLayout.setVgap(0);
        flowLayout.setAlignment(0);
        setLayout(flowLayout);
        setSize(getWidthComponent(), 22);
        if (create) {
            add(getCreateJButton(), null);
        }
        if (open) {
            add(getOpenJButton(), null);
        }
        if (save) {
            add(getSaveJButton(), null);
        }
        if (modify) {
            add(getModifyJButton(), null);
        }
        if (remove) {
            add(getRemoveJButton(), null);
        }
        if (print) {
            add(getPrintJButton(), null);
        }
        if (help) {
            add(getHelpJButton(), null);
        }
    }

    private int getWidthComponent() {
        int x = 0;
        if (create) {
            x++;
        }
        if (open) {
            x++;
        }
        if (save) {
            x++;
        }
        if (modify) {
            x++;
        }
        if (remove) {
            x++;
        }
        if (print) {
            x++;
        }
        if (help) {
            x++;
        }
        return 22 * x;
    }

    private JButton getPrintJButton() {
        if (printJButton == null) {
            printJButton = new JButton();
            printJButton.setPreferredSize(new Dimension(18, 18));
            printJButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/printer.png")));
            printJButton.setToolTipText(Messages.getString("IconBar.print"));
            printJButton.setContentAreaFilled(false);
            printJButton.setBorderPainted(false);
            printJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    fireEvent(new AnswerEvent(IconBar.this, 5));
                }
            });
        }
        return printJButton;
    }

    private JButton getModifyJButton() {
        if (modifyJButton == null) {
            modifyJButton = new JButton();
            modifyJButton.setPreferredSize(new Dimension(18, 18));
            modifyJButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/modify.png")));
            modifyJButton.setToolTipText(Messages.getString("IconBar.modify"));
            modifyJButton.setContentAreaFilled(false);
            modifyJButton.setBorderPainted(false);
            modifyJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    fireEvent(new AnswerEvent(IconBar.this, 3));
                }
            });
        }
        return modifyJButton;
    }

    private JButton getRemoveJButton() {
        if (removeJButton == null) {
            removeJButton = new JButton();
            removeJButton.setPreferredSize(new Dimension(18, 18));
            removeJButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/delete.png")));
            removeJButton.setToolTipText(Messages.getString("IconBar.delete"));
            removeJButton.setContentAreaFilled(false);
            removeJButton.setBorderPainted(false);
            removeJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    fireEvent(new AnswerEvent(IconBar.this, 4));
                }
            });
        }
        return removeJButton;
    }

    private JButton getSaveJButton() {
        if (saveJButton == null) {
            saveJButton = new JButton();
            saveJButton.setPreferredSize(new Dimension(18, 18));
            saveJButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/save.png")));
            saveJButton.setToolTipText(Messages.getString("IconBar.save"));
            saveJButton.setContentAreaFilled(false);
            saveJButton.setBorderPainted(false);
            saveJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    fireEvent(new AnswerEvent(IconBar.this, 2));
                }
            });
        }
        return saveJButton;
    }

    private JButton getCreateJButton() {
        if (createJButton == null) {
            createJButton = new JButton();
            createJButton.setPreferredSize(new Dimension(18, 18));
            createJButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/new.png")));
            createJButton.setToolTipText(Messages.getString("IconBar.new"));
            createJButton.setBorderPainted(false);
            createJButton.setContentAreaFilled(false);
            createJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    fireEvent(new AnswerEvent(IconBar.this, 0));
                }
            });
        }
        return createJButton;
    }

    private JButton getOpenJButton() {
        if (openJButton == null) {
            openJButton = new JButton();
            openJButton.setPreferredSize(new Dimension(18, 18));
            openJButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/open.png")));
            openJButton.setToolTipText(Messages.getString("IconBar.open"));
            openJButton.setBorderPainted(false);
            openJButton.setContentAreaFilled(false);
            openJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    fireEvent(new AnswerEvent(IconBar.this, 1));
                }
            });
        }
        return openJButton;
    }

    private JButton getHelpJButton() {
        if (helpJButton == null) {
            helpJButton = new JButton();
            helpJButton.setPreferredSize(new Dimension(18, 18));
            helpJButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/help.png")));
            helpJButton.setToolTipText(Messages.getString("IconBar.help"));
            helpJButton.setContentAreaFilled(false);
            helpJButton.setBorderPainted(false);
            helpJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    fireEvent(new AnswerEvent(IconBar.this, 6));
                }
            });
        }
        return helpJButton;
    }

    public void setEnabledCreate(boolean b) {
        createJButton.setEnabled(b);
    }

    public void setEnabledOpen(boolean b) {
        openJButton.setEnabled(b);
    }

    public void setEnabledSave(boolean b) {
        saveJButton.setEnabled(b);
    }

    public void setEnabledModify(boolean b) {
        modifyJButton.setEnabled(b);
    }

    public void setEnabledRemove(boolean b) {
        removeJButton.setEnabled(b);
    }

    public void setEnabledPrint(boolean b) {
        printJButton.setEnabled(b);
    }

    public void setEnabledHelp(boolean b) {
        helpJButton.setEnabled(b);
    }

    public void setBackgroundButtons(Color c) {
        createJButton.setBackground(c);
        openJButton.setBackground(c);
        saveJButton.setBackground(c);
        modifyJButton.setBackground(c);
        removeJButton.setBackground(c);
        printJButton.setBackground(c);
        helpJButton.setBackground(c);
    }

    public void addAnswerListener(AnswerListener l) {
        listeners.addElement(l);
    }

    public void removeAnswerListener(AnswerListener l) {
        listeners.removeElement(l);
    }

    public void fireEvent(AnswerEvent e) {
        Vector list = (Vector) listeners.clone();
        for (int i = 0; i < list.size(); i++) {
            AnswerListener listener = (AnswerListener) list.elementAt(i);
            switch (e.getID()) {
                case 0: // '\0'
                    listener.create(e);
                    break;

                case 1: // '\001'
                    listener.open(e);
                    break;

                case 2: // '\002'
                    listener.save(e);
                    break;

                case 3: // '\003'
                    listener.modify(e);
                    break;

                case 4: // '\004'
                    listener.remove(e);
                    break;

                case 5: // '\005'
                    listener.print(e);
                    break;

                case 6: // '\006'
                    listener.help(e);
                    break;
            }
        }

    }
    private Vector listeners;
    private JButton helpJButton;
    private JButton openJButton;
    private JButton createJButton;
    private JButton saveJButton;
    private JButton removeJButton;
    private JButton modifyJButton;
    private JButton printJButton;
    private boolean help;
    private boolean open;
    private boolean create;
    private boolean save;
    private boolean remove;
    private boolean modify;
    private boolean print;
}
